package com.wangwei.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangwei.entity.Product;
import com.wangwei.entity.ProductSwiperImage;
import com.wangwei.service.IProductService;
import com.wangwei.service.IProductSwiperImageService;
import com.wangwei.vo.ApiResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
  商品Controller
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Resource
    private IProductService productService;

    @Resource
    private IProductSwiperImageService productSwiperImageService;

    /**
     * 查询轮播商品
     *
     * @return
     */
    @GetMapping("/findSwiper")
    public ApiResult findSwiper() {
        // 查询数据
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<Product>().eq("isSwiper", true).orderByAsc("swiperSort");
        List<Product> swiperProductList = productService.list(productQueryWrapper);
        // 返回
        Map<String, Object> map = new HashMap<>();
        map.put("message", swiperProductList);
        return ApiResult.ok(map);
    }

    /**
     * 查询热门推荐商品8个
     *
     * @return
     */
    @GetMapping("/findHot")
    public ApiResult findHot() {
        Page<Product> page = new Page<>(0, 8);
        Page<Product> pageProduct = productService.page(page, new QueryWrapper<Product>().eq("isHot", true).orderByAsc("hotDateTime"));
        List<Product> hotProductList = pageProduct.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("message", hotProductList);
        return ApiResult.ok(map);
    }

    /**
     * 根据id查询商品信息
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public ApiResult detail(Integer id) {
        Product product = productService.getById(id);
        List<ProductSwiperImage> productSwiperImageList = productSwiperImageService.list(new QueryWrapper<ProductSwiperImage>().eq("productId", product.getId()).orderByAsc("sort"));
        product.setProductSwiperImageList(productSwiperImageList);
        Map<String, Object> map = new HashMap<>();
        map.put("message", product);
        return ApiResult.ok(map);
    }

    /**
     * 商品搜索
     *
     * @param q
     * @return
     */
    @GetMapping("/search")
    public ApiResult search(String q) {
        List<Product> productList = productService.list(new QueryWrapper<Product>().like("name", q));
        Map<String, Object> map = new HashMap<>();
        map.put("message", productList);
        return ApiResult.ok(map);
    }


}
